﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class XOSO
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		//private SMS_LIST WML;
		ArrayList arr = new ArrayList();
		//DateTime datetime;
		public XOSO(SMS_MO MO)
		{
			WMO = MO;
		}

		public XOSO()
		{
		}
		#endregion
		#region Method
		public void run()
		{
			XOSO_WAIT xs_wait = new XOSO_WAIT();
			WMT = new SMS_MT(WMO);			
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });
			if(WMO.ServiceID.Replace("04","")=="6172")
			{				
				if(tmpMsg.Trim()=="")//lay ket qua moi nhat
				{
					if(DateTime.Now.Hour>=19 && !CheckCurrentResult())//Neu nhan sau 7h ma chua co ket moi nhat thi o che do cho
					{
						xs_wait.InsetSMS_WAIT(WMO.MO_ID,WMO.UserID,WMO.ServiceID,WMO.CommandCode,tmpMsg,WMO.RequestID,WMO.RequestTime,0,1);
					}
					else
					{
						string strReturnMsg = GetReturnMsg();
						if(strReturnMsg!="")
						{
							WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
							WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
							WMT.Insert(); 
						}
					}
				}
				else if(msg[0].Trim().ToUpper()=="CHO")//cho nhan ket qua
				{
					xs_wait.InsetSMS_WAIT(WMO.MO_ID,WMO.UserID,WMO.ServiceID,WMO.CommandCode,tmpMsg,WMO.RequestID,WMO.RequestTime,0,1);
				}
			}
				//************tuong that truc tiep
			else if(WMO.ServiceID.Replace("04","")=="6572")
			{
				xs_wait.InsetSMS_WAIT(WMO.MO_ID,WMO.UserID,WMO.ServiceID,WMO.CommandCode,tmpMsg,WMO.RequestID,WMO.RequestTime,1,8);
			}
				//************cho nhan ket qua 21 ngay lien tiep
			else if(WMO.ServiceID.Replace("04","")=="6772")
			{
				xs_wait.InsetSMS_WAIT(WMO.MO_ID,WMO.UserID,WMO.ServiceID,WMO.CommandCode,tmpMsg,WMO.RequestID,WMO.RequestTime,0,21);
			}
			else
			{	
				string msgbd="Soan tin:\n";
				msgbd +="XSTD gui 6172 nhan kq moi nhat\n";
				msgbd +="XSTD gui 6572 nhan kq tuong thuat\n";
				msgbd +="XSTD gui 6772 nhan kq 21 ngay";
				WMT.AddMessage(WMO.UserID, msgbd, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
		}
		private string GetReturnMsg()
		{			
			string msgBody= String.Empty;			
			string connString = ConfigurationSettings.AppSettings["STOCK_CONNECTION_STRING"].ToString();
			string strSQl = "SELECT TOP 1 * FROM XOSO WHERE Mamien='MB' ORDER BY Ngay DESC"	;
			SqlConnection oledbConn = new SqlConnection(connString);			
			SqlDataAdapter oleda  = new SqlDataAdapter(strSQl, oledbConn);
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();				
			oledbConn.Open();// Open connection
			oleda.Fill(ds, "VIT");
			oledbConn.Close();
			dt = ds.Tables["VIT"];								
			DateTime dateTime = DateTime.Parse(dt.Rows[0]["Ngay"].ToString());
			if (ds.Tables[0].Rows.Count>0)
			{						
				msgBody = string.Concat("TD:",string.Concat(dateTime.Day,"/",dateTime.Month),"\n",
					"DB:",dt.Rows[0]["GiaiDacBiet"].ToString().Trim().Replace(" ",""),"\n",
					"1:",dt.Rows[0]["GiaiNhat"].ToString().Trim().Replace(" ",""),"\n",
					"2:",dt.Rows[0]["GiaiNhi"].ToString().Trim().Replace(" ",""),"\n",
					"3:",dt.Rows[0]["GiaiBa"].ToString().Trim().Replace(" ",""),"\n",
					"4:",dt.Rows[0]["GiaiTu"].ToString().Trim().Replace(" ",""),"\n",
					"5:",dt.Rows[0]["GiaiNam"].ToString().Trim().Replace(" ",""),"\n",
					"6:",dt.Rows[0]["GiaiSau"].ToString().Trim().Replace(" ",""),"\n",
					"7:",dt.Rows[0]["GiaiBay"].ToString().Trim().Replace(" ",""));
			}
			else
			{
				msgBody = ConfigSettings.ReadSetting("SMS_HELP_XS");
			}			
			ds.Dispose();		
			return msgBody;
		}
		private Boolean CheckCurrentResult()
		{
			Boolean val=false;
			string connString = ConfigurationSettings.AppSettings["STOCK_CONNECTION_STRING"].ToString();
			string strSQl = "SELECT ID FROM XOSO WHERE Mamien='MB' and convert(nvarchar(10),Ngay,103)=convert(nvarchar(10),getdate(),103)"	;
			SqlConnection oledbConn = new SqlConnection(connString);			
			SqlDataAdapter oleda  = new SqlDataAdapter(strSQl, oledbConn);
			DataSet ds = new DataSet();			
			oledbConn.Open();// Open connection
			oleda.Fill(ds, "VIT");
			oledbConn.Close();
			if(ds.Tables["VIT"].Rows.Count>0)		
				val=true;
			return val;
		}
		#endregion
	}
}
